package com.sunkyc.framework.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunkyc.framework.system.entity.SysDeptAncestors;
import com.sunkyc.framework.system.mapper.SysDeptAncestorsMapper;
import com.sunkyc.framework.system.service.SysDeptAncestorsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门祖级id表 服务实现类
 * </p>
 *
 * @author sunkyc
 * @since 2022-06-08
 */
@Service
@RequiredArgsConstructor
public class SysDeptAncestorsServiceImpl extends ServiceImpl<SysDeptAncestorsMapper, SysDeptAncestors> implements SysDeptAncestorsService {
    private final SysDeptAncestorsMapper basemapper;

    @Override
    public List<SysDeptAncestors> queryListByDeptId(Long deptId) {
        return basemapper.selectList( Wrappers.lambdaQuery(SysDeptAncestors.class)
                .eq(SysDeptAncestors::getDeptId,deptId));
    }

    @Override
    public int delByDeptId(Long deptId) {
        return basemapper.delete(Wrappers.lambdaQuery(SysDeptAncestors.class).eq(SysDeptAncestors::getDeptId,deptId));
    }

    @Override
    public Boolean checkedIsChildren(Long deptId,Long parentId) {
        List<SysDeptAncestors> sysDeptAncestors = queryListByDeptId(parentId);
        for (SysDeptAncestors sysDeptAncestor : sysDeptAncestors) {
            if(sysDeptAncestor.getDeptAncestorId().equals(deptId)){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Long> queryChildrenIds(Long deptId) {
        List<Long> list = new ArrayList<>();
        List<SysDeptAncestors> sysDeptAncestors = basemapper.selectList(Wrappers.lambdaQuery(SysDeptAncestors.class)
                .eq(SysDeptAncestors::getDeptAncestorId, deptId));
        if(CollectionUtils.isNotEmpty(sysDeptAncestors)){
            sysDeptAncestors.forEach(item->list.add(item.getDeptId()));
        }

        return list;
    }
}
